from re import I
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from Ui_home import IndexView
from Ui_setting import SettingView
from Ui_Start import StartView
from component.Config import Config
import os,time
from component.voice_recorder.settings import Settings
import component.util as util
import _thread

class MainView(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainView, self).__init__()
        self.initConfig()
        self.showIndexView()

    def showSettingView(self):
        self.settingView = SettingView(self)
        self.settingView.back.clicked.connect(self.showIndexView)
        self.settingView.begin.clicked.connect(self.showStartView)

    def showStartView(self):
        if not Config.currentTestName:
            Config.currentTestName = self.settingView.lineEdit.text() or '默认复试名称'
        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        Config.beginTime = currentTime
        self.startView = StartView(self)
        self.startView.testFinish.connect(self.testFinishEvent)

    def showIndexView(self):
        self.indexView = IndexView(self)
        if self.indexView.begin_btn.text() == '继续复试':
            self.indexView.begin_btn.clicked.connect(self.showStartView)
        else:
            self.indexView.begin_btn.clicked.connect(self.showSettingView)


    # 复试结束事件    
    def testFinishEvent(self):

        # 录音目录
        baseDir = os.path.join(Config.getBaseDir(),Config.currentTestName)
        record_dir = os.path.join(baseDir,'录音文件')

        self.outputLog(baseDir)

        Config.removeConfig()
        Config.resetConfig()
        self.showIndexView()
        _thread.start_new_thread(self.outputDocument,(record_dir,))

    # 输出复试日志
    def outputLog(self,dir):
        if not os.path.exists(dir):
            os.mkdir(dir)
        with open(os.path.join(dir, f'复试日志.txt'), 'w') as f:
            content = f'复试名称：{Config.currentTestName}\n复试时间：{Config.beginTime}\n' \
                      f'复试总人数：{len(Config.students)}\n' \
                      f'复试完成人数:{len(Config.completedStudentIndex)}\n' \
                      f'缺席复试人数：{len(Config.skipedStudentIndex)}\n'
            f.write(content)


    # 输出复试记录
    def outputDocument(self,dir):
        if not os.path.exists(dir):
            return
        self.indexView.startOutput()
        print('正在转文字。。。')
        self.settings = Settings()
        # 录音目录
        folders = os.listdir(dir)

        for fold in folders:
            if '.' in fold:
                continue
            context = ''
            foldPath = os.path.join(dir, str(fold))
            p1 = os.path.join(foldPath, Config.prefix[1] + '.wav')
            p2 = os.path.join(foldPath, Config.prefix[2] + '.wav')
            p3_1 = os.path.join(foldPath, Config.prefix[3] + '1.wav')
            p3_2 = os.path.join(foldPath, Config.prefix[3] + '2.wav')
            p3_3 = os.path.join(foldPath, Config.prefix[3] + '3.wav')
            p3_4 = os.path.join(foldPath, Config.prefix[3] + '4.wav')
            p3_5 = os.path.join(foldPath, Config.prefix[3] + '5.wav')
            p4 = os.path.join(foldPath, Config.prefix[4] + '.wav')

            context += '听力部分：\n'
            context += util.voice2Text(p1)
            context += '\n'
            context += '自我介绍：\n'
            context += util.voice2Text(p2)
            context += '\n'
            context += '专业提问：\n'
            context += '\n'
            context += '第1题：\n'
            context += util.voice2Text(p3_1)
            context += '\n'
            context += '第2题：\n'
            context += util.voice2Text(p3_2)
            context += '\n'
            context += '第3题：\n'
            context += util.voice2Text(p3_3)
            context += '\n'
            context += '第4题：\n'
            context += util.voice2Text(p3_4)
            context += '\n'
            context += '第5题：\n'
            context += util.voice2Text(p3_5)
            context += '\n'
            context += '自由问答：\n'
            context += util.voice2Text(p4)

            with open(os.path.join(foldPath, f'{fold}-output.txt'), 'w') as f:
                f.write(context)
        print('转文字成功')
        self.indexView.endOutput()

    def initConfig(self):
        # 读取复试名单
        Config.prepareStudent()
        # 读取听力材料
        Config.prepareListenStuff()
        # 读取问题
        Config.prepareProblems()
        # 读取配置文件
        Config.loadConfig()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    m = MainView()
    m.show()
    # m.outputDocument()
    # t=TimerWidget()
    # t.show()


    sys.exit(app.exec_())
